/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package action.activity;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import po.Activity;
import po.User;
import java.util.List;
import service.ActivityService;
import myUtils.MemberInfo;
import service.OrgService;
/**
 *
 * @author HX
 */
public class CreateActivityFormAction extends ActionSupport implements ServletRequestAware
{
    private HttpServletRequest request;
    private ActivityService activityService;
    private OrgService orgService;
    private List<MemberInfo> members;
    private int orgId;
    private String orgName;

    @Override
    public String execute()
    {
        String msg="";
        if (ActionContext.getContext().getSession().get("user") == null) {
            msg = "you should login first";
            request.setAttribute("msg", msg);
            return ERROR;
         }
        if(request.getParameter("orgId")==null)
         {
             msg="no id!!!";
             request.setAttribute("msg", msg);
             return ERROR;
         }
        if(request.getParameter("orgName")==null)
         {
             msg="no org name!!!";
             request.setAttribute("msg", msg);
             return ERROR;
         }
        orgId = Integer.parseInt(request.getParameter("orgId"));
        orgName = request.getParameter("orgName");
        if(!orgService.isLeader(((User) ActionContext.getContext().getSession().get("user")).getId(), orgId)){
            msg = "you can not create an activity!";
            request.setAttribute("msg", msg);
            return ERROR;
        }
        List list = activityService.getMembers(orgId);
        Iterator it = list.iterator();
        members = new ArrayList<MemberInfo>();
        while(it.hasNext()){
            Object[] o=(Object[])it.next();
            members.add(new MemberInfo((Integer)o[0],(String)o[1]));
        }
        request.setAttribute("members", members);

        if(members==null)
        {
            msg="null  members!!!";
            request.setAttribute("msg", msg);
            return ERROR;
        }
        else{
            return SUCCESS;
        }

    }

    public void setActivityService(ActivityService activityService)
    {
        this.activityService=activityService;
    }

    public ActivityService getActivityService()
    {
        return this.activityService;
    }

    public void setOrgService(OrgService orgService)
    {
        this.orgService=orgService;
    }

    public OrgService getOrgService()
    {
        return this.orgService;
    }

    public void setServletRequest(HttpServletRequest request)
    {
        this.request=request;
    }

    public void setMembers(List members){
        this.members = members;
    }

    public List getMembers(){
        return this.members;
    }

    public void setOrgId(int orgId){
        this.orgId = orgId;
    }

    public int getOrgId(){
        return this.orgId;
    }

    public void setOrgName(String orgName){
        this.orgName = orgName;
    }

    public String getOrgName(){
        return this.orgName;
    }

}
